﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.OleDb;

namespace eSilky.DAL
{
    public class Group
    {
        public DataTable GetGroups()
        {

            OleDbConnection con;
            string qryString;
            OleDbDataAdapter da;
            DataSet ds;

            try
            {
                con = dBConnection.GetConnection();
                qryString = "select grp_id,grp_name,grp_description,grp_status from esilky_group_master";
                da = new OleDbDataAdapter(qryString, con);
                ds = new DataSet();
                da.Fill(ds);
                if (ds.Tables.Count > 0)
                {
                    return (ds.Tables[0]);
                }
                else
                {
                   return new DataTable();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        #region Save Group
        /// <summary>
        /// Save Group
        /// </summary>
        /// <param name="objCategory"></param>
        /// <returns></returns>
        public bool SaveGroup(VO.GroupMaster objGroup)
            {
            OleDbConnection con = null;
            string qryString = "";
            OleDbCommand comObj = null;
            try
                {
                con = dBConnection.GetConnection();

                con = dBConnection.GetConnection();
                con.Open();
                qryString = "INSERT INTO esilky_group_master (grp_name,grp_description,grp_status)" +
                    "VALUES (@name,@description,@status)";
                comObj = new OleDbCommand(qryString, con);
                comObj.Parameters.Add(new OleDbParameter("@name", objGroup.name));
                comObj.Parameters.Add(new OleDbParameter("@description", objGroup.description));
                comObj.Parameters.Add(new OleDbParameter("@status", true));
                return Convert.ToBoolean(comObj.ExecuteNonQuery());
                }
            catch (Exception)
                {
                throw;
                }
            finally
                {
                con.Close();
                }
            }
        #endregion

        #region Update Group
        /// <summary>
        /// Update Group
        /// </summary>
        /// <param name="objCategory"></param>
        /// <returns></returns>
        public bool UpdateGroup(VO.GroupMaster objGroup)
            {
            OleDbConnection con = null;
            string qryString = "";
            OleDbCommand comObj = null;
            try
                {
                con = dBConnection.GetConnection();

                con = dBConnection.GetConnection();
                con.Open();
                qryString = "UPDATE esilky_group_master SET grp_name= @name,grp_description = @description,grp_status = @status" +
                    " WHERE grp_id = @id";
                comObj = new OleDbCommand(qryString, con);
                comObj.Parameters.Add(new OleDbParameter("@name", objGroup.name));
                comObj.Parameters.Add(new OleDbParameter("@description", objGroup.description));
                comObj.Parameters.Add(new OleDbParameter("@status", objGroup.status));
                comObj.Parameters.Add(new OleDbParameter("@id", objGroup.id));
                return Convert.ToBoolean(comObj.ExecuteNonQuery());
                }
            catch (Exception ex)
                {
                throw ex;
                }
            finally
                {
                con.Close();
                }
            }
        #endregion

        #region Delete Group
        /// <summary>
        /// Delete Group
        /// </summary>
        /// <param name="objCategory"></param>
        /// <returns></returns>
        public bool DeleteGroup(int grp_id)
            {
            OleDbConnection con = null;
            string qryString = "";
            OleDbCommand comObj = null;
            try
                {
                con = dBConnection.GetConnection();

                con = dBConnection.GetConnection();
                con.Open();
                qryString = "DELETE FROM esilky_group_master  WHERE grp_id = @id";
                comObj = new OleDbCommand(qryString, con);
                comObj.Parameters.Add(new OleDbParameter("@id", grp_id));
                return Convert.ToBoolean(comObj.ExecuteNonQuery());
                }
            catch (Exception ex)
                {
                throw ex;
                }
            finally
                {
                con.Close();
                }
            }
        #endregion
    }
}